package edu.mobbuzz.page.menu.category;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Category;
import edu.mobbuzz.facade.CategoryFacade;
import edu.mobbuzz.facade.LabelFacade;
import edu.mobbuzz.page.menu.CategoryMenu;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.page.menu.rsschannel.RSSChannelChoice;
import edu.mobbuzz.renderer.CategoryRenderer;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;
import java.util.Vector;

public class CategoryPage extends Menu {
    private Form categoryForm;
    private Container categoryPanel;
    private Label menuTitle;
    private DefaultListModel categoryListModel;
    private List categoryList;    
    private Command backCmd;
    private Command deleteCmd;
    private Command deleteAllCmd;
    private Command editCmd;
    private Command newCmd;
    private Command detailCmd;
    private CategoryFacade categoryFacade;
    private LabelFacade labelFacade;
    
    public CategoryPage(){
        categoryForm = new Form();
        categoryPanel = new Container();
        menuTitle = new Label(getMenuTitle());

        categoryListModel = new DefaultListModel(getCategoryFacade().displayCategoryList());
        
        detailCmd = new Command("Detail"){
            public void actionPerformed(ActionEvent evt) {
               openMessageByCategory();
            }
        };
        
        newCmd = new Command("New Category") {
            public void actionPerformed(ActionEvent evt) {
               editCategory(ModeString.NEW);
            }
        };
        
        editCmd = new Command("Edit Category") {
            public void actionPerformed(ActionEvent evt) {
               editCategory(ModeString.EDIT);
            }
        };
        
        deleteCmd = new Command("Delete Category") {
            public void actionPerformed(ActionEvent evt) {
               deleteCategory();
            }
        };
        
        deleteAllCmd = new Command("Delete All Category"){
            public void actionPerformed(ActionEvent evt) {
                deleteAllCategory();
            }
               
        };        
        
        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToCategoryMenu();
            }
        };
        
    }
    public void showPage() {
       
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);         
              
        categoryPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));                 
        categoryPanel.addComponent(menuTitle);
        categoryPanel.addComponent(createList(categoryListModel, List.VERTICAL, new CategoryRenderer()));
            
        categoryForm.setLayout(new BorderLayout());       
        categoryForm.addComponent(BorderLayout.CENTER, categoryPanel);
        categoryForm.addCommand(backCmd);
        
        if(categoryListModel.getSize()!=0){                    
            categoryForm.addCommand(deleteAllCmd);
            categoryForm.addCommand(deleteCmd);
            categoryForm.addCommand(editCmd);
            
        }
        
        categoryForm.addCommand(newCmd);
        
        if(categoryListModel.getSize()!=0){
            categoryForm.addCommand(detailCmd);
        }
        
        categoryForm.setCommandListener(getActionlistener());
        categoryForm.setScrollableY(true);
        categoryForm.show();
    }
    
    private List createList(DefaultListModel categoryListModel, int orientation, ListCellRenderer renderer) {
        categoryList = new List(categoryListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };        
        categoryList.getStyle().setBgTransparency(0);
        categoryList.getStyle().setBorder(Border.getEmpty());
        categoryList.setListCellRenderer(renderer);
        categoryList.setOrientation(orientation);
        
        return categoryList;
    }
    
    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);
        
        if(game == Display.GAME_FIRE){
           if(categoryListModel.getSize()==0){
                editCategory(ModeString.NEW);
            }else{
                
            }
        }else if(game == Display.GAME_DOWN){
            if(categoryList.getSelectedIndex()<(categoryList.size()-1)){
               categoryList.setSelectedIndex(categoryList.getSelectedIndex()+1);
            }else{
                categoryList.setSelectedIndex(0);
            }
           
        }else if(game == Display.GAME_UP){
             if(categoryList.getSelectedIndex()!=0){
                 categoryList.setSelectedIndex(categoryList.getSelectedIndex()-1);
             }else{
                 categoryList.setSelectedIndex(categoryList.size()-1);
             }
           
        }
    }
    
      
    
    public void openMessageByCategory(){
        RSSChannelChoice rssChannelChoice = new RSSChannelChoice();
        rssChannelChoice.setExitCmd(getExitCmd());
        rssChannelChoice.setActionlistener(getActionlistener());
        rssChannelChoice.showPage();
        
    }
    public void editCategory(int mode) {
        CategoryForm editCategoryForm = new CategoryForm();
        editCategoryForm.setMode(mode);
        editCategoryForm.setExitCmd(getExitCmd());
        editCategoryForm.setActionlistener(getActionlistener());
        if(mode==ModeString.EDIT){
            editCategoryForm.setCategory(((Category)categoryList.getSelectedItem()));
            editCategoryForm.setIndex(categoryList.getSelectedIndex());
        }
        editCategoryForm.showPage();
    }

    public void deleteCategory(){
        TextArea body = new TextArea("Delete Category ?", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {
                deleteLabel((Category)categoryList.getSelectedItem());
                getCategoryFacade().deleteCategory(categoryList.getSelectedIndex());
                categoryListModel.removeItem(categoryList.getSelectedIndex());
                
                if(categoryListModel.getSize()==0){
                    categoryForm.removeCommand(deleteCmd);
                    categoryForm.removeCommand(deleteAllCmd);
                    categoryForm.removeCommand(detailCmd);
                }


            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);

    }

    public void deleteAllCategory(){
        TextArea body = new TextArea("Delete All Category ?", 2, 5);
        body.setGrowByContent(true);
        Command noCommand = new Command("No");
        Command yesCommand = new Command("Yes"){
            public void actionPerformed(ActionEvent evt) {                
                for(int i=0;i<categoryListModel.getSize();i++){
                    getCategoryFacade().deleteCategory(i);
                }
                if(categoryListModel.getSize()==0){
                    categoryForm.removeCommand(deleteCmd);
                    categoryForm.removeCommand(deleteAllCmd);
                    categoryForm.removeCommand(detailCmd);
                }
            }
        };
        Dialog.show("", body, new Command[]{noCommand, yesCommand}, Dialog.TYPE_CONFIRMATION, null,0,null);

    }

    public void deleteLabel(Category category){        
        Vector indexCategoryArr = new Vector();
        indexCategoryArr = getLabelFacade().getDeletedIndexLabelByCategory(category.getRecId());
        for(int i=0;i<indexCategoryArr.size();i++){
            getLabelFacade().deleteLabel(Integer.parseInt(indexCategoryArr.elementAt(i).toString()));
        }
    }
    public void backToCategoryMenu(){
        getCategoryFacade().closeCategoryRS();
        CategoryMenu categoryMenu = new CategoryMenu();
        categoryMenu.setExitCmd(getExitCmd());
        categoryMenu.setActionlistener(getActionlistener());
        categoryMenu.showPage();
    }

    public CategoryFacade getCategoryFacade() {
        if(categoryFacade==null){
            categoryFacade = new CategoryFacade();
        }
        return categoryFacade;
    }

    public LabelFacade getLabelFacade() {
        if(labelFacade==null)
            labelFacade = new LabelFacade();
        return labelFacade;
    }
    
    public String getMenuTitle() {
        return MenuString.CATEGORY;
    }

    

    
}
 
